TopicChartPortlet = Class.extend({
	init: function()	{
		this.name = "TopicChartPortlet";
	},
	
	onBegin: function() {
		this.registerObserver();
	},
	
	onEnd: function() {
		this.unregisterObserver();
	},
	
	onReloadPage: function() {
		this.run();
	},
	
	run: function() {
		var obj = this;
		var to = getWeek(new Date());
		var from = to - 16;
		var id = this.getRequest().getParam('id');
		this.onAjax('catchword', 'get-single-stats', {id: id, type: 'week', from: from, to: to}, 'get', {
			onSuccess: function(ret) {
				obj.getPortletPlaceholder().paintCanvas(obj.render());
				var data = {};
				data.cat = Array();
				data.answers = Array();
				data.questions = Array();
				data.vote = Array();
				data.delay = Array();
				var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
				
				for(var i=from;i<=to;i++)	{
					log(i/4);
					var month = months[Math.ceil(i/4.34523809524)-1];
					var w = i%4+1;
					data.cat.push(month+' W'+w);
					data.answers.push(0);
					data.questions.push(0);
					data.vote.push(0);
					data.delay.push(-1);
				}
				
				for(var i in ret.data)	{
					var cur = ret.data[i];
					data.answers[cur.week-from] = cur.answers;
					data.questions[cur.week-from] = cur.questions;
					data.vote[cur.week-from] = cur.vote;
					data.delay[cur.week-from] = cur.delay;
				}
				
				obj.data = data;
				obj.options = {
					chart: {
						renderTo: obj.name+"-Container",
						defaultSeriesType: 'line',
						marginRight: 130,
						marginBottom: 25
					},
					title: {
						text: 'Weekly data analysis for '+ret.topic,
						x: -20 //center
					},
					subtitle: {
						text: 'Auto-generated by Asking.vn Report Service',
						x: -20
					},
					xAxis: {
						categories: data.cat
					},
					yAxis: {
						title: {
							text: 'Number of activities'
						},
						plotLines: [{
							value: 0,
							width: 1,
							color: '#808080'
						}]
					},
					legend: {
						layout: 'vertical',
						align: 'right',
						verticalAlign: 'top',
						x: -10,
						y: 100,
						borderWidth: 0
					},
					tooltip: {
						formatter: function() {
							if (this.series.name == "Answers No.")
								return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +' answers';
							if (this.series.name == "Questions No.")
								return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +' questions';
							if (this.series.name == "Vote No.")
								return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +' vote';
							if (this.series.name == "Avg. Waiting Time")	{
								if (this.y < 1 && this.y > 0)
									return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ Math.ceil(this.y*24) +' hrs';
								if (this.y == -1)
									return '<b>'+ this.series.name +'</b><br/>'+ this.x +': N/A';
								return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +' days';
							}
							return "";
						}
					},
					series: [{
						name: 'Answers No.',
						data: data.answers
					}, {
						name: 'Questions No.',
						data: data.questions
					}, {
						name: 'Vote No.',
						data: data.vote
					}, {
						name: 'Avg. Waiting Time',
						data: data.delay
					}]
				};
				
				obj.chart = new Highcharts.Chart(obj.options);
			}
		});
	},
	
	reloadOptions: function(checkedId, name, data)	{
		if (!this.requestForEffectiveResource(checkedId).is(':checked'))	{
			var series = Array();
			for(var i in this.options.series)	{
				if (this.options.series[i].name == name)	{
					continue;
				}
				series.push(this.options.series[i]);
			}
			this.options.series = series;
		} else {
			this.options.series.push({name: name, data: this.data[data]});
		}
	},
	
	onTopicAnalysisAnswerChecked: function() {
		this.reloadOptions('AnswerCheck', 'Answers No.', 'answers');
		this.chart = new Highcharts.Chart(this.options);
	},
	
	onTopicAnalysisQuestionChecked: function() {
		this.reloadOptions('QuestionCheck', 'Questions No.', 'questions');
		this.chart = new Highcharts.Chart(this.options);
	},
	
	onTopicAnalysisDelayChecked: function() {
		this.reloadOptions('DelayCheck', 'Avg. Waiting Time', 'delay');
		this.chart = new Highcharts.Chart(this.options);
	},
	
	onTopicAnalysisVoteChecked: function() {
		this.reloadOptions('VoteCheck', 'Vote No.', 'vote');
		this.chart = new Highcharts.Chart(this.options);
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);